package com.example.shopping1902.me;


import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shopping1902.R;
import com.example.shopping1902.utils.BaseFragment;
import com.example.shopping1902.utils.SaveUserUtils;
import com.example.shopping1902.utils.URLUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class LoginFragment extends BaseFragment implements View.OnClickListener {
    EditText telEt,pwdEt;
    Button logBtn;
    TextView sinaTv;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_login, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {

        telEt=view.findViewById(R.id.frag_reg_et1);
        pwdEt=view.findViewById(R.id.frag_reg_et2);
        logBtn=view.findViewById(R.id.frag_login_btn);
        sinaTv=view.findViewById(R.id.frag_login_tv_sina);
        sinaTv.setOnClickListener(this);
        logBtn.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.frag_login_btn:
                String tel=telEt.getText().toString();
                String pwd=pwdEt.getText().toString();
                if (checkInfo(tel,pwd)){
                    return;
                }
                Map<String,String>map=new HashMap<>();
                map.put("phone",tel);
                map.put("password",pwd);
                postNetwork(URLUtils.loginURL,map);//联网
                break;
            case R.id.frag_login_tv_sina:

                break;
        }
    }
    @Override
    public void onSuccess(String result) {
        UserBean bean=new Gson().fromJson(result,UserBean.class);
        int code=bean.getCode();
        switch (code){
            case 200:
                Toast.makeText(getContext(), "登录成功", Toast.LENGTH_SHORT).show();
                UserBean.DataBean dataBean=bean.getData();
                SaveUserUtils.saveUserInfo(getContext(),dataBean);//保持用户信息
                //跳转到个人中心页面
                Intent intent = new Intent(getActivity(),PersonCenterActivity.class);
                startActivity(intent);
                getActivity().finish();//关闭当前页面
                break;
            case 201:
                Toast.makeText(getContext(), "输入用户名或者密码错误！", Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(getContext(), "参数错误，登录失败！", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private boolean checkInfo(String tel, String pwd) {
        if (TextUtils.isEmpty(tel) | TextUtils.isEmpty(pwd)) {
            Toast.makeText(getContext(), "输入内容不能为空", Toast.LENGTH_SHORT).show();
            return true;
        }
        String match = "^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}$";
        if (!tel.matches(match)) {
            Toast.makeText(getContext(), "输入电话号码格式不对", Toast.LENGTH_SHORT).show();
            telEt.setText("");
            return true;
        }
        return false;
    }
}
